﻿#ifndef MAINWINDOW_H
#define MAINWINDOW_H

#include <qmath.h>

#include <QAbstractButton>
#include <QAccelerometerReading>
#include <QCheckBox>
#include <QClipboard>
#include <QGeoServiceProvider>
#include <QGeoServiceProviderFactory>
#include <QInputMethod>
#include <QSplashScreen>
#include <QtConcurrent/QtConcurrentRun>

#ifdef Q_OS_ANDROID

#include <QCoreApplication>
#include <QJniEnvironment>
#include <QJniObject>

#endif

#include <QBarCategoryAxis>
#include <QBarSeries>
#include <QBarSet>
#include <QChart>
#include <QChartView>
#include <QComboBox>
#include <QDateTime>
#include <QDebug>
#include <QDesktopServices>
#include <QDir>
#include <QElapsedTimer>
#include <QFileDialog>
#include <QGyroscope>
#include <QHBoxLayout>
#include <QHeaderView>
#include <QInputDialog>
#include <QLineEdit>
#include <QMainWindow>
#include <QMenu>
#include <QMessageBox>
#include <QParallelAnimationGroup>
#include <QPropertyAnimation>
#include <QPushButton>
#include <QQmlApplicationEngine>
#include <QQmlContext>
#include <QQmlEngine>
#include <QQuickWidget>
#include <QRandomGenerator>
#include <QScatterSeries>
#include <QScrollBar>
#include <QScroller>
#include <QSensorManager>
#include <QSettings>
#include <QSplineSeries>
#include <QSplitter>
#include <QStringList>
#include <QTextEdit>
#include <QThread>
#include <QTimer>
#include <QToolTip>
#include <QTreeWidgetItem>
#include <QValueAxis>

#include "MainHelper.h"
#include "src/AboutThis.h"
#include "src/CategoryList.h"
#include "src/CloudBackup.h"
#include "src/Comm/Method.h"
#include "src/Comm/ReceiveShare.h"
#include "src/Comm/ShowMessage.h"
#include "src/Comm/TextEditToolbar.h"
#include "src/Comm/inputmethodreset.h"
#include "src/EditRecord.h"
#include "src/Exercise/Steps.h"
#include "src/Exercise/StepsOptions.h"
#include "src/LoadPic.h"
#include "src/MyThread.h"
#include "src/Notes/Notes.h"
#include "src/Notes/NotesList.h"
#include "src/Preferences.h"
#include "src/Reader/Reader.h"
#include "src/Reader/ReaderSet.h"
#include "src/Report.h"
#include "src/Todo/Todo.h"
#include "src/Todo/TodoAlarm.h"
#include "ui_AboutThis.h"
#include "ui_CategoryList.h"
#include "ui_CloudBackup.h"
#include "ui_DateSelector.h"
#include "ui_Notes.h"
#include "ui_NotesList.h"
#include "ui_Preferences.h"
#include "ui_ShowMessage.h"
#include "ui_StepsOptions.h"
#include "ui_Todo.h"
#include "ui_TodoAlarm.h"

class SaveThread;
class ReadChartThread;
class ReadTWThread;
class ReadEBookThread;
class BakDataThread;
class ImportDataThread;
class SearchThread;
class UpdateGpsMapThread;
class SliderButton;

#include <QMetaType>

QT_BEGIN_NAMESPACE
namespace Ui {
class MainWindow;
}
QT_END_NAMESPACE

class MainWindow : public QMainWindow {
  Q_OBJECT

 public:
  MainWindow(QWidget* parent = nullptr);
  ~MainWindow();
  Ui::MainWindow* ui;

  QMenu* mainMenu = nullptr;

  int x, y, w, h;

  QInputDialog* m_RenameDlg = nullptr;

  QString labelNormalStyleSheet = R"(/* 动态适配明暗模式 */
    QLabel {
        background-color: qlineargradient(
            x1:0, y1:0, x2:1, y2:0,
            stop:0 palette(light),  /* 明色端 */
            stop:1 palette(mid)     /* 过渡色 */
        );
        color: palette(window-text); /* 跟随系统文本色 */
        padding: 5px;
        border-radius: 4px;         /* 可选：增加圆角提升质感 */
    })";

  QString labelEnSyncStyleSheet =
      "background-color:qlineargradient(spread:pad,x1:1,y1:0,x2:0,y2:0,stop:0 "
      "#87CEFF,stop:1 #FFAEB9); color:black;selection-background-color: "
      "lightblue;";

  QString labelEncStyleSheet =
      "QLabel { "
      "background-color: qlineargradient(spread:pad, x1:0, y1:0, x2:1, y2:0, "
      "stop:0 rgba(255, 204, 204, 255), stop:1 rgba(220, 220, 220, 255)); "
      "color: black; }";

  QString labelSyncStyleSheet =
      "QLabel { "
      "background-color: qlineargradient(spread:pad, x1:0, y1:0, x2:1, y2:0, "
      "stop:0 #87CEFF, stop:1 rgba(220, 220, 220, 255)); "
      "color: black; }";

  QString mySearchText = "";

  bool isOpenSearchResult = false;

  bool isEditItem = false;
  bool isDelItem = false;

  QString strLatestModify = tr("None");

  QDialog* dlgProg = nullptr;

  void setMini();

  int days = 45;
  int newHeight;
  QInputMethod* pAndroidKeyboard = QApplication::inputMethod();

  bool isMemoVisible;
  bool isReaderVisible;
  QString treeStyle = "QTreeWidget::item {height: 28;}";
  int mainHeight = 0;

  int alertWindowsCount = 0;
  QString strText;
  QString strRefreshUrl;
  QString CurrentYear = "20xx";
  qreal curx = 0;
  bool isMousePress = false;
  bool isMouseMove = false;

  bool initMain = false;

  QString LatestTime = tr("Latest Time");
  QStringList timeLines;
  QString loginTime;

  int mwh;

  QString listStyle =
      "QListWidget{item-background: "
      "rgb(244,23,24); item-color:rgb(255,255,255); "
      "border-radius:2px;border:2px "
      "solid red;}";
  int sRate = 0;
  qreal ax, ay, az, gx, gy, gz;
  int testCount1 = 0;
  int testCount = 0;
  int smallCount = 0;

  qlonglong CurTableCount = 0;

  static void get_Today(QTreeWidget*);
  SaveThread* mySaveThread;
  ReadChartThread* myReadChartThread;
  ReadTWThread* m_ReadTWThread;
  ReadEBookThread* myReadEBookThread;
  BakDataThread* myBakDataThread;
  ImportDataThread* myImportDataThread;
  SearchThread* mySearchThread;
  UpdateGpsMapThread* myUpdateGpsMapThread;
  static void ReadChartData();
  static int get_Day(QString date);
  static QString get_Year(QString date);
  static QString get_Month(QString date);
  void init_Options();

  QStringList chartCategories;
  QList<QVariant> qmlFreqValues;
  QList<QVariant> qmlAmountValues;

  AboutThis* m_AboutThis;
  EditRecord* m_EditRecord;
  Todo* m_Todo;
  Report* m_Report;
  Preferences* m_Preferences;

  Reader* m_Reader;
  TodoAlarm* m_TodoAlarm;
  DateSelector* m_DateSelector = nullptr;

  ReceiveShare* m_ReceiveShare;
  MainHelper* m_MainHelper;

  QList<QTreeWidgetItem*> findItemList;
  bool isFindTextChange = false;
  int findPos = 0;

  QTimer* timer;

  QTimer* timerMousePress;
  QTimer* timerSyncData;
  QTimer* tmeStartRecordAudio;

  static void readData(QTreeWidget*);

  static void saveTab();
  bool isSlide = false;
  void init_TotalData();
  void modify_Data();
  void add_Data(QTreeWidget*, QString, QString, QString);
  bool del_Data(QTreeWidget*);
  static QTreeWidget* get_tw(int tabIndex);

  static QStringList get_MonthList(QString strY, QString strM);
  static void drawMonthChart();

  static void SaveFile(QString);

  static void init_Stats(QTreeWidget*);

  void startSave(QString);
  void startRead(QString);
  static void drawDayChart();
  static void readDataInThread(int ExceptIndex);

  void stopJavaTimer();

  QString getYMD(QString date);
  bool bakData();
  bool importBakData(QString fileName);

  QString getTabText();

  void refreshMainUI();

  QString getSelectedText();

  int calcStringPixelWidth(QString s_str, QFont font, int n_font_size);
  int calcStringPixelHeight(QFont font, int n_font_size);

  void delItem(int index);
  int getCount();
  void clearAll();
  void reloadMain();
  void setCurrentIndex(int index);
  void gotoEnd();
  void gotoIndex(int index);
  void setItemHeight(int h);

  QString getTop(int index);
  QString getText0(int index);
  int getItemType(int index);
  int getCurrentIndex();
  bool setTWCurrentItem();
  QString getText1(int index);
  QString getText2(int index);

  void startInitReport();

  void startSyncData();

  int getMaxDay(QString sy, QString sm);
  void showProgress();

  void setScrollBarPos(double pos);

  void addItem(QString text0, QString text1, QString text2, QString text3,
               int itemH);

  void execDeskShortcut();

  void on_DelayCloseProgressBar();

  void clearWidgetFocus();

  int max_day = 31;

  void execNeedSyncNotes();

  void saveNeedSyncNotes();

  void updateMainTab();

  void on_btnPageUp_clicked();
  void on_btnPageNext_clicked();
  void on_btnBackDir_clicked();

  void on_btnUpload_clicked();
  void on_btnDownload_clicked();

 protected:
  void closeEvent(QCloseEvent* event) override;
  bool eventFilter(QObject* watch, QEvent* evn) override;
  void paintEvent(QPaintEvent* event) override;
  void changeEvent(QEvent* event) override;
  void resizeEvent(QResizeEvent* event) override;

  void hideEvent(QHideEvent* event) override;

 public slots:
  void on_btnAutoStop_clicked();

  void on_btnMove_clicked();

  void on_btnSelText_clicked();

  void on_hSlider_sliderMoved(int position);
  void clickMainTab();

  void on_btnTodo_clicked();
  void readEBookDone();

  void on_btnShowFindNotes_clicked();

  void on_btnRename_clicked();

  void on_actionOneDriveBackupData();

  void on_actionReport_triggered();

  void on_btnCopy_clicked();

  void on_btnSearch_clicked();

  void on_btnCancelSel_clicked();

  void on_timerSyncData();
  void timerUpdate();

  void on_actionRename_triggered();

  void on_actionAdd_Tab_triggered();

  void on_actionDel_Tab_triggered();

  void on_tabWidget_currentChanged(int index);

  void on_btnModifyRecord_clicked();

  void on_actionExport_Data_triggered();

  void on_actionImport_Data_triggered();

  void on_twItemClicked();

  void on_twItemDoubleClicked();

  void on_actionAbout();

  void on_btnFind_clicked();

  void on_actionFind_triggered();

  void on_rbFreq_clicked();

  void on_rbAmount_clicked();

  void saveDone();

  void bakDataDone();

  void importDataDone();

  void readChartDone();

  void readTWDone();

  void searchDone();

  void on_actionPreferences_triggered();

  void on_tabCharts_currentChanged(int index);

  void on_btnSteps_clicked();

  void on_rbSteps_clicked();

  void on_btnSelTab_clicked();

  void on_btnReader_clicked();

  void on_btnBackReader_clicked();

  void on_btnOpen_clicked();

  void on_btnPages_clicked();

  void on_btnReadList_clicked();

  void on_actionBakFileList();

  void on_btnBack_One_clicked();

  void on_btnBackNotesGraph_clicked();

  void on_btnNotes_clicked();

  void on_btnZoomIn_clicked();

  void on_btnZoomOut_clicked();

  void on_openKnotBakDir();

  void reeditData();

  void clickData();

  void on_btnStartSearch_clicked();

  void on_btnChart_clicked();

  void on_btnBack_NotesSearchResult_clicked();

  void on_btnOpenSearchResult_clicked();

  void on_btnEditNote_clicked();

  void on_btnOpenNote_clicked();

  void on_btnStartDate_clicked();

  void on_btnEndDate_clicked();

  void on_ExecShortcut();

  void on_btnFindNotes_clicked();

  void on_btnShowBookmark_clicked();

  void on_btnBackBookList_clicked();

  void on_btnBackEditRecord_clicked();

  void on_btnBackNoteList_clicked();

  void on_btnBackReaderSet_clicked();

  void on_btnBackNoteRecycle_clicked();

  void on_timerMousePress();

  void on_btnBackImg_clicked();

  void on_btnReport_clicked();

  void on_btnAdd_clicked();

  void on_btnDel_clicked();

  void on_KVChanged();

  void on_btnAddTodo_clicked();

  void on_btnBackTodo_clicked();

  void on_btnHigh_clicked();

  void on_btnLow_clicked();

  void on_btnSetTime_clicked();

  void on_btnRecycle_clicked();

  void on_btnReturnRecycle_clicked();

  void on_btnClearRecycle_clicked();

  void on_btnDelRecycle_clicked();

  void on_btnRestoreRecycle_clicked();

  void on_editTodo_textChanged();

  void on_btnBackSteps_clicked();

  void on_btnReset_clicked();

  void on_btnBack_Report_clicked();

  void on_btnYear_clicked();

  void on_btnMonth_clicked();

  void on_btnCategory_clicked();

  void closeProgress();

  void selTab();

  void updateGpsMapDone();

  void on_btnBackTabRecycle_clicked();

  void on_btnBackSearch_clicked();

  void on_btnBackBakList_clicked();

  void on_btnBackSetTab_clicked();

  void on_btnBack_Tree_clicked();

  void on_btnCancelType_clicked();

  void on_actionTabRecycle();

  void on_actionShareFile();

  void on_btnUpMove_clicked();

  void on_btnDownMove_clicked();

  void on_btnDelNote_NoteBook_clicked();

  void on_btnMoveTo_clicked();

  void on_btnToPDF_clicked();

  void on_btnManagement_clicked();

  void on_btnNoteRecycle_clicked();

 private slots:
  void on_btnMenu_clicked();

  void on_btnSync_clicked();

  void on_btnPasteTodo_clicked();

  void on_btnClearSearchText_clicked();

  void on_btnImportBakList_clicked();

  void on_btnOkViewCate_clicked();

  void on_btnDelTabRecycle_clicked();

  void on_btnRestoreTab_clicked();

  void on_btnDelBakFile_clicked();

  void on_btnDelNoteRecycle_clicked();

  void on_btnRestoreNoteRecycle_clicked();

  void on_btnFindPreviousNote_clicked();

  void on_btnFindNextNote_clicked();

  void on_btnClearNoteFindText_clicked();

  void on_btnNoteBookMenu_clicked();

  void on_btnNoteMenu_clicked();

  void on_btnOkType_clicked();

  void on_btnDelType_clicked();

  void on_btnRenameType_clicked();

  void on_btnType_clicked();

  void on_btnOkEditRecord_clicked();

  void on_btnClearType_clicked();

  void on_btnClearDetails_clicked();

  void on_btnClearAmount_clicked();

  void on_editAmount_textChanged(const QString& arg1);

  void on_editCategory_textChanged(const QString& arg1);

  void on_editDetails_textChanged();

  void on_hsH_valueChanged(int value);

  void on_hsM_valueChanged(int value);

  void on_btn7_clicked();

  void on_btn8_clicked();

  void on_btn9_clicked();

  void on_btn4_clicked();

  void on_btn5_clicked();

  void on_btn6_clicked();

  void on_btn1_clicked();

  void on_btn2_clicked();

  void on_btn3_clicked();

  void on_btn0_clicked();

  void on_btnDot_clicked();

  void on_btnDel_Number_clicked();

  void on_btnOkBookList_clicked();

  void on_btnClearAllRecords_clicked();

  void on_btnAnd_clicked();

  void on_btnClear_clicked();

  void on_btnModify_clicked();

  void on_btnChartMonth_clicked();

  void on_btnChartDay_clicked();

  void on_btnTabMoveUp_clicked();

  void on_btnTabMoveDown_clicked();

  void on_btnHideFind_clicked();

  void on_btnStepsOptions_clicked();

  void on_btnRecentOpen_clicked();

  void on_btnMenuReport_clicked();

  void on_btnCatalogue_clicked();

  void on_btnRemoveBookList_clicked();

  void on_ReceiveShare();

  void on_btnShareImage_clicked();

  void on_btnDelImage_clicked();

  void on_btnSetBookmark_clicked();

  void on_btnFontLess_clicked();

  void on_btnFontPlus_clicked();

  void on_btnFont_clicked();

  void on_btnBackgroundColor_clicked();

  void on_btnForegroundColor_clicked();

  void on_editBackgroundColor_textChanged(const QString& arg1);

  void on_editForegroundColor_textChanged(const QString& arg1);

  void on_btnStyle1_clicked();

  void on_btnStyle2_clicked();

  void on_btnStyle3_clicked();

  void on_btnGoPage_clicked();

  void on_hSlider_sliderReleased();

  void on_CloseProgressBar();

  void on_btnShareBook_clicked();

  void slotSetBookmark();
  void on_btnAutoRun_clicked();

  void on_btnLessen_clicked();

  void on_btnDefault_clicked();

  void on_btnPlus_clicked();

  void on_btnAddTodo_pressed();

  void on_btnAddTodo_released();

  void on_tmeFlash();

  void on_btnClearReaderFont_clicked();

  void on_StartRecordAudio();

  void on_sliderPlayAudio_sliderPressed();

  void on_sliderPlayAudio_sliderReleased();

  void on_btnGPS_clicked();

  void on_btnSelGpsDate_clicked();

  void on_btnGetGpsListData_clicked();

  void on_rbCycling_clicked();

  void on_rbHiking_clicked();

  void on_rbRunning_clicked();

  void on_btnWebDAVBackup_clicked();

  void on_btnWebDAVRestore_clicked();

  void on_chkWebDAV_clicked();

  void on_editFindNote_returnPressed();

  void on_btnClearSearchResults_clicked();

  void on_btnFindNotes2_clicked();

  void on_btnOpenEditFind_clicked();

  void on_btnTools_clicked();

  void on_btnCopyNoteLink_clicked();

  void on_cboxWebDAV_currentTextChanged(const QString& arg1);

  void on_btnShowCboxList_clicked();

  void on_btnRotation_clicked();

  void on_btnBackNoteDiff_clicked();

  void on_btnSendEmail_clicked();

  void on_btnShareBakFile_clicked();

  void on_btnNewNote_clicked();

  void on_btnShareBookText_clicked();

  void on_btnAddBookNote_clicked();

  void on_btnViewBookNote_clicked();

  void on_btnMap_clicked();

  void on_btnSportsChart_clicked();

 private:
  bool isMoveEntry;
  QTimer* tmeFlash;
  int nFlashCount = 0;
  QString keyType;
  bool isShowDetails = false;
  QString strShowDetails;
  bool isTabChanged = false;

  int frameChartHeight;

  qreal aoldX, aoldY, aoldZ;
  int countOne = 0;

  void on_btnHideKey_clicked();

  void resetWinPos();

  void init_Instance();

  void getMainTabs();

  QString strTime, strAmount, strCategory, strDetails;

  void init_Thread_Timer();

 signals:
};

#endif  // MAINWINDOW_H
